<?php
/**
 * @title 449. 序列化和反序列化二叉搜索树 - 层次遍历
 * @author start2004
 */

class Codec {
    function __construct() {

    }

    /**
     * @param TreeNode $root
     * @return String
     */
    function serialize($root) {
        /**
         * @since 2020-07-28 特殊情况排除
         */
        if($root === null){
            return "[]";
        } else {}

        /**
         * @since 2020-07-28 返回集合数组
         */
        $arr = [];

        /**
         * @since 2020-07-28 根节点
         */
        $arr[] = $root->val;
        $nexts = [$root];

        /**
         * @since 2020-07-28 一层一层往下遍历节点
         */
        while($nexts){
            $nextTmp = [];

            foreach ($nexts as $leaf){
                /**
                 * @since 2020-07-28 左节点
                 */
                if($leaf->left === null){
                    $arr[] = null;
                } else {
                    $arr[] = $leaf->left->val;
                    $nextTmp[] = $leaf->left;
                }

                /**
                 * @since 2020-07-28 右节点
                 */
                if($leaf->right === null){
                    $arr[] = null;
                } else {
                    $arr[] = $leaf->right->val;
                    $nextTmp[] = $leaf->right;
                }
            }

            /**
             * @since 2020-07-28 下次循环
             */
            $nexts = $nextTmp;
        }

        /**
         * @since 2020-07-28 去掉尾部null
         */
        for ($i=count($arr)-1; $i>=0; $i--){
            if($arr[$i] === null){
                unset($arr[$i]);
            } else {
                break;
            }
        }

        /**
         * @return
         */
        return json_encode($arr);
    }

    /**
     * @param String $data
     * @return TreeNode
     */
    function deserialize($data) {
        /**
         * @since 2020-07-28 转数组
         */
        $arr = json_decode($data, true);

        /**
         * @since 2020-07-28 数组长度
         */
        $len = count($arr);

        /**
         * @since 2020-07-28 排除特殊情况
         */
        if($len == 0){
            return null;
        } else {}

        /**
         * @since 2020-07-28 根节点
         */
        $root = new TreeNode($arr[0]);
        $nexts = [$root];

        /**
         * @since 2020-07-28 遍历
         */
        $i = 1;
        while($nexts){
            $nextTmp = [];

            /**
             * @since 2020-07-28 遍历每个节点
             */
            foreach ($nexts as $leaf){
                /**
                 * @since 2020-07-28 左节点
                 */
                if($i>=$len){
                    $nextTmp = [];
                    break;
                } else {
                    if ($arr[$i] !== null) {
                        $leaf->left = new TreeNode($arr[$i]);
                        $nextTmp[] = $leaf->left;
                    }
                    $i++;
                }

                /**
                 * @since 2020-07-28 右节点
                 */
                if($i>$len){
                    $nextTmp = [];
                    break;
                } else {
                    if ($arr[$i] !== null) {
                        $leaf->right = new TreeNode($arr[$i]);
                        $nextTmp[] = $leaf->right;
                    }
                    $i++;
                }
            }

            /**
             * @since 2020-07-28 下次循环
             */
            $nexts = $nextTmp;
        }

        /**
         * @return
         */
        return $root;
    }
}




/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.28_2.php
 */
$datas = [
    [1,2,3,null,4],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new CodeC();
    $result = $obj->deserialize(json_encode($data));
    var_dump($result);
    $result = $obj->serialize($result);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
